<?php

namespace App\Model\Order;

use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Model\Member\MemberModel;
use App\Model\Member\MemberProfitScoreModel;
use App\Model\Member\MemberScoreModel;
use App\Util\UserIdentityHelper;
use EasySwoole\Component\Di;
use App\Model\Member\VipModel;
use EasySwoole\Redis\Redis;
/**
 * CREATE TABLE `ss_order` (
 * `id` char(24) NOT NULL COMMENT '订单id',
 * `user_id` int(11) NOT NULL COMMENT '用户id',
 * `order_amount` decimal(10,2) NOT NULL COMMENT '订单实付金额',
 * `total_profit` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '订单总提成',
 * `user_profit` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '当前用户提成',
 * `referrer_profit` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '团队提成',
 * `profit_chain` json DEFAULT NULL COMMENT '推荐链分级提成',
 * `order_state` char(10) NOT NULL DEFAULT 'UNSHIPPING' COMMENT '订单状态 CREATED:结算中;CANCELLED:已取消;SETTLED:已结算',
 * `order_date` date DEFAULT NULL COMMENT '订单日期',
 * `order_time` datetime DEFAULT NULL COMMENT '下单时间',
 * `cancel_time` datetime NOT NULL COMMENT '取消时间',
 * `settle_time` datetime DEFAULT NULL COMMENT '结算时间',
 * `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '创建时间',
 * `created_by` varchar(32) NOT NULL DEFAULT '' COMMENT '创建人员账号',
 * `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
 * `updated_by` varchar(32) NOT NULL DEFAULT '' COMMENT '最后更新人员账号',
 * `is_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除[0:未删除，1：已删除]',
 * `deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '删除时间',
 * PRIMARY KEY (`id`) USING BTREE,
 * KEY `user_id` (`user_id`) USING BTREE,
 * KEY `created_at` (`created_at`) USING BTREE
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='结算订单';
*/
/**
 * Class OrderModel
 * @property int id
 * @property int user_id
 * @property string order_amount
 * @property string total_profit
 * @property string user_profit
 * @property string referrer_profit
 * @property string profit_chain
 * @property string order_state
 * @property string order_date
 * @property string order_time
 * @property string cancel_time
 * @property string settle_time
 * @property string created_at
 * @property string created_by
 * @property string updated_at
 * @property string updated_by
 * @property int is_delete
 * @property string deleted_at
 */
class OrderModel extends BaseModel
{

    protected $tableName = 'ss_order';

    protected $casts = [
        'id'            => 'integer',
        'user_id'       => 'integer',
        'order_amount'  => 'float',
        'total_profit'  => 'float',
        'user_profit'   => 'float',
        'referrer_profit' => 'float',
        'profit_chain'  => 'json',
        'order_state'   => 'string',
        'order_date'   => 'string',
        'order_time'    => 'string',
        'cancel_time'   => 'string',
        'settle_time'   => 'string',
        'created_at'    => 'string',
        'created_by'    => 'string',
        'updated_at'    => 'string',
        'updated_by'    => 'string',
        'is_delete'     => 'integer',
        'deleted_at'    => 'string',
    ];
    //订单状态 UNSHIPPING:待发货;SHIPPED:已发货;RECEIPT:确认收货;AUDITING:退货审核中;AUDITED:退货审核通过;AFTERSALES:退货售后;RETURNED:已退货
    const CREATED = 'CREATED';//结算中
    const CANCELLED    = 'CANCELLED';//已取消
    const SETTLED    = 'SETTLED';//已结算

    const PROFITRANGE = [1.5, 1.5]; //默认提成
    const MAXPROFIT = 1.5;//提成最大值
    const MAXREFERRERPROFIT = 2.5;//下级提成最大值
    const ORDERLIMIT = 100;//订单限制默认提成
    const MAXPLOCKEDDAY = 9999;//提成最大不可提现天数
    const ORDERLIMITTIME = 10;//下单时间间隔

    public static function getOrderStateDict($key = null)
    {
        return self::getMap([
            'CREATED' => '结算中',
            'CANCELLED' => '已取消',
            'SETTLED' => '已结算',
        ], $key);
    }

    public function member()
    {
        return $this->hasOne(MemberModel::class, function ($query) {
            $query->where('is_delete', 0);
            return $query;
        }, 'user_id', 'id');
    }
    public function memberScore()
    {
        return $this->hasMany(MemberScoreModel::class, function ($query) {
            $query->join(MemberModel::create()->getTableName() . ' m', 'm.id=user_id', 'INNER');
            $query->fields(MemberScoreModel::create()->getTableName() . '.*, m.username');
            $query->where(MemberScoreModel::create()->getTableName() . '.is_delete', 0);
            return $query;
        }, 'id', 'object_id');
    }
    public function memberProfitScore()
    {
        return $this->hasMany(MemberProfitScoreModel::class, function ($query) {
            $table = MemberProfitScoreModel::create()->getTableName();
            $query->join(MemberModel::create()->getTableName() . ' m', 'm.id=user_id', 'INNER');
            $query->fields($table . '.user_id, m.username, '. $table .'.score, '. $table .'.type, '. $table .'.remark, '. $table .'.object_id');
            $query->where(MemberProfitScoreModel::create()->getTableName() . '.is_delete', 0);
            return $query;
        }, 'id', 'object_id');
    }
    protected static function onAfterUpdate($model, $res)
    {
        self::delCache('orderCache:');
    }

    protected static function onAfterInsert($model, $res)
    {
        self::delCache('orderCache:');
    }

}
